/**
 * Created by Administrator on 2020/5/18.
 */
;
var helper = require("../../javascript/helper");
var request = require("request");
var db = require("../../javascript/mysql");

var name = "境外滑雪运动保险";
var url = "http://www.epicc.com.cn/eproduct/conproposal/calculate.do";
var mysql = new db.mysqlClient("accident");
//生成产品数据
function genProductInfo() {
    plan = {
        "planA": {
            //年龄段
            "year": ["4-70"],
            //保障天数
            "days": ["1d", "7d", "30d"],
            //保障人数
            "people_count": ["1"],
            //意外伤害
            "accident_death": ["100000"],
            //紧急医务治疗
            "exigency_back": ["20000"],
            //门诊医疗费用补偿
            "accident_medical": ["10000"],
            //个人责任
            "public_duty": ["10000"],
            //个人行李
            "luggage": ["1200"]
        },
        "planB": {
            //年龄段
            "year": ["4-70"],
            //保障天数
            "days": ["1d", "7d", "30d"],
            //保障人数
            "people_count": ["1"],
            //意外伤害
            "accident_death": ["200000"],
            //紧急医务治疗
            "exigency_back": ["30000"],
            //门诊医疗费用补偿
            "accident_medical": ["30000"],
            //个人责任
            "public_duty": ["20000"],
            //个人行李
            "luggage": ["6000"]

        },
        "planC": {
            //年龄段
            "year": ["4-70"],
            //保障天数
            "days": ["1d", "7d", "30d"],
            //保障人数
            "people_count": ["1"],
            //意外伤害
            "accident_death": ["500000"],
            //紧急医务治疗
            "exigency_back": ["50000"],
            //门诊医疗费用补偿
            "accident_medical": ["50000"],
            //个人责任
            "public_duty": ["40000"],
            //个人行李
            "luggage": ["12000"]
        }

    };
    for (k in plan) {
        data = helper.product([plan[k]["year"], plan[k]["days"], plan[k]["people_count"], plan[k]["accident_death"], plan[k]["exigency_back"], plan[k]["accident_medical"], plan[k]["public_duty"], plan[k]["luggage"]])
        for (i = 0; i < data.length; i++) {
            dt = helper.formatProductInfo(data[i], plan[k]);
            httpRequest(dt);
        }
    }
}
//执行请求
function httpRequest(dt) {
    if(dt.days=="1d"){
        endDate = "2020/05/19"
    }else if(dt.days=="7d"){
        endDate = "2020/05/25"
    }else {
        endDate = "2020/06/17"
    }
    if(dt.accident_death == "100000"){
        items = "1517812174773"
    }else if(dt.accident_death=="200000"){
        items = "1517812174774"
    }else {
        items = "1517812174775"
    }
    params = {
        "startdate": "2020/05/19",
        "enddate":endDate,
        "items":items,
        "peoplecount": "1",
        "planextendvalue": "",
        "productcode": "LAY_W"
    };
    request({
        url: url,
        method: "POST",
        json: false,
        headers: {
            "Accept": "application/json, text/plain, */*",
            "Accept-Language":"zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
            "Cookie": "epicc_tid=epicc.1578282117557422; _gscu_321070471=7828211745c42110; s_fid=51BB77C19E20D92E-16021A351FFE693E; vid=ef0bea9d6c25528ee3c6714b85e0baef; epicc_ntid=AAAAAV6CpgMv3h/HwX4WAg==; svid=0C46E643B8EB2B5549E7AA5CA65A65FB; ag_fid=PqyKLr1Kk08UPYuF; xY6qiPJDsR=MDAwM2IyNTRmNDgwMDAwMDAwMjUwI0gYCwUxNTg5Nzc4MzQ1; epiccnewEbsWeb=J6cmJoyvZK4MGjX8KZDQ2WHKSXF8G0QZNfDH2WLr0iVwzg4WX1ne!511019310; epiccnewEbsWebeproduct=lyomJo-eQXVGRMKXG_UsAllr4G2sU5ioJkPI_6DQhWFRr_NPC2lc!-1799868909; YXeuzFnfi6=MDAwM2IyNTRmNDgwMDAwMDAwNzYwfF0qQRYxNTg5Nzc4MzQ2; personalCenter=LcwmLI1OrF07-9xK0wInyS1GXMHMIzrZqP7Dc7-mgBr26PaQ2HEh!-1953081442; _gscbrs_321070471=1; udmp_cm_sign_321070471=1; s_cc=true; R4XlxlrGUk=MDAwM2IyNTRmNDgwMDAwMDAwNWUwXBkJPW0xNTg5NzgxMDIz; s_getNewRepeat=1589781032025-Repeat; s_vnum=1609818118180%26vn%3D20; s_invisit=true; _gscs_321070471=t89781026d355lc64|pv:1; _gscm=1; trkHmClickCoords=470%2C410%2C3696%2Chttp%3A%2F%2Fwww.epicc.com.cn%2Fyiwaibaoxian%2Fjwhxzhbzbx%2F%23%2Fmain",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36"
        },
        body: helper.urlencode(params)
    }, function (err, res, body) {
        if (!err && res.statusCode == 200) {
            price = (JSON.parse( body )[3].premium.toFixed(2)).toString();
            rs = {
                "name":name,
                "company":"中国人民保险",
                "price":price
            };
            result = Object.assign(dt,rs);
            mysql.insertData( result );

        }
    })

}



